<?php


namespace App\Http\Controllers;
use App\Models\News;
use App\Service\ApiResponse;
use Illuminate\Http\Request;

class NewsController
{
    public function list()
    {
        $model = new News();
        $where = [
            'is_delete'=>0,
            'state' => 1,
        ];
        $list = $model::select(['id as news_id','type','title','years'])
            ->where($where)
            ->where('issue_time','<=',date('Y-m-d H:i:s'))
            ->orderBy('news_date','desc')
            ->get();
        $result = [];
        foreach($list as $key => $val) {
            if (empty($result[$val['years']])){
                $result[$val['years']] = [
                    'year' => $val['years'].'年',
                    'list' => [$val]
                ];
            }else{
                $result[$val['years']]['list'] = array_merge($result[$val['years']]['list'],[$val]);
            }
        }
        $result = array_values($result);

        return ApiResponse::success($result);
    }

    public function detail()
    {
        $params = $_POST;
        $id = $params['id'];

        $model = new News();
        $where = [
            'id'=>$id,
            'is_delete'=>0,
        ];
        $result = $model::select(['id','type','title','years as year','keyword','news_date as date','description','content'])
            ->where($where)
            ->first();
        $result->year = $result->year.'年';
        return ApiResponse::success($result);
    }
}
